package org.ing.sisval.da;

import java.sql.Connection;
import java.util.ArrayList;
import org.ing.sisval.be.BEUnitario;
import org.rx.cr.dao.DAOAbstract;

public class DAUnitario extends DAOAbstract<BEUnitario> {

    public DAUnitario(Connection conn) {
        setConnection(conn);
    }

    @Override
    public int registrar(BEUnitario be) throws Exception {
        try {
            setStoreProcedure("SISVAL.sp_ins_unitario");
            setParameterString(be.getCategoria());
            setParameterString(be.getTipo());
            setParameterString(be.getDescripcion());
            setParameterDouble(be.getPrecio());

            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int actualizarRegistro(BEUnitario be) throws Exception {
        try {
            setStoreProcedure("SISVAL.sp_upd_unitario");
            setParameterInt(be.getUnitarioid());
            setParameterString(be.getCategoria());
            setParameterString(be.getTipo());
            setParameterString(be.getDescripcion());
            setParameterDouble(be.getPrecio());
            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    public int eliminarRegistro(int unitarioid) throws Exception {
        try {
            setStoreProcedure("SISVAL.sp_del_unitario");
            setParameterInt(unitarioid);
            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public ArrayList<BEUnitario> buscarReferencia(String string) throws Exception {
        try {
            clear();
            setStoreProcedure("SISVAL.sp_sel_unitario");
            setParameterString(string);
            commitc();
            while (existResult()) {
                BEUnitario be = new BEUnitario();
                be.setUnitarioid(getDataInt(1));
                be.setCategoria(getDataString(2));
                be.setTipo(getDataString(3));
                be.setDescripcion(getDataString(4));
                be.setPrecio(getDataDouble(5));
                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    public ArrayList<BEUnitario> listar(int id) throws Exception {
        try {
            clear();
            setStoreProcedure("SISVAL.sp_sel_unitario");
            setParameterInt(id);
            commitc();
            while (existResult()) {
                BEUnitario be = new BEUnitario();
                be.setUnitarioid(getDataInt(1));
                be.setCategoria(getDataString(2));
                be.setTipo(getDataString(3));
                be.setDescripcion(getDataString(4));
                be.setPrecio(getDataDouble(5));
                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public BEUnitario buscar(int i) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ArrayList<BEUnitario> listar() throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public int eliminarRegistro(BEUnitario tipo) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
